//
//  CSPayCenterViewController.m
//  SHCloudStore
//
//  Created by SH_iOS on 2017/4/12.
//  Copyright © 2017年 SH. All rights reserved.
//
//3.统一支付接口：POST
#define KCSUSER_DO_PAYMENT  @"/payment_base/api/user_do_payment"

#import "SHUnionPayModel.h"
#import "CSPaymentWayHeader.h"
#import "UserModel.h"
#import "CSQrcodeWebViewController.h"
//支付密码框
#import "PassWordView.h"
//设置支付密码
#import "SHUserPayPasswordController.h"
//充值输入
#import "ToppedCentreViewController.h"

//银联支付或者京东支付
#import "JDPayViewController.h"
//添加银联支付银行卡
#import "SHAddPayBankCardController.h"
//支付卡选择列表
#import "PayBankListView.h"
//新京东支付页面
#import "CSJDPayViewController.h"
//支付方式模型
#import "CSCustomPayWaysModel.h"
//支付方选择列表
#import "CSPayListView.h"
#import "CSPaymentWayCell.h"
#import "CSTransferPayController.h"
//支付 充值中心
#import "CSPayCenterViewController.h"

@interface CSPayCenterViewController ()<CSPayListViewDelegate,PayBankListViewDelegate,PopPasswordViewDelegate,UITableViewDelegate,UITableViewDataSource,CSPaymentWayCellDelegate>
//new
@property(nonatomic,strong)UIView *fistBgView;
@property(nonatomic,strong)UIView *SecondBgView;
@property(nonatomic,strong)UIView *tabViewHeader;
@property(nonatomic,strong)UIView *myBgView;

@property(nonatomic,strong)UIImageView  *icon;
@property(nonatomic,strong)UILabel *usertypeLabel;
@property(nonatomic,strong)UILabel *paymoneyLabel;
@property(nonatomic,strong)UILabel *orderidLabel;
@property(nonatomic,strong)UILabel *paymoneyNumLabel;
@property(nonatomic,strong)UILabel *orderidNumLabel;
@property(nonatomic,strong)UIButton *paymentBtn;
@property(nonatomic,strong)NSMutableArray *paymentDataArray;
@property(nonatomic,strong)PassWordView *passwordView;
@property(nonatomic,strong)NSString  *balance;
@property(nonatomic,assign)BOOL  isSourceOrder;
@property(nonatomic,assign)int   index;

@property(nonatomic,strong)UITableView *paymentwayTable;

@property(nonatomic,strong)NSMutableArray *unionPayArray;//银联支付类型
@property(nonatomic,copy)NSString *webPayTitle;//付款页面标题

@end

@implementation CSPayCenterViewController{
    
    NSInteger   customPaymentWay;//支付类型
    NSString   *_qrcode_url;//扫码支付二维码连接
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    
    return UIStatusBarStyleDefault;
    
}

// ======== 分割线 =========
- (NSMutableArray *)paymentDataArray{
    
    if (!_paymentDataArray) {
        _paymentDataArray =  [NSMutableArray arrayWithCapacity:0];
    }
    return _paymentDataArray;
}

static NSString *const paymentwayTableReuseId = @"paymentwayTableReuseId";

- (UITableView *)paymentwayTable{
       if (!_paymentwayTable) {
        
        _paymentwayTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _paymentwayTable.rowHeight = __I6Height(58);
        _paymentwayTable.delegate = self;
        _paymentwayTable.dataSource = self;
        _paymentwayTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_paymentwayTable registerClass:[CSPaymentWayCell class] forCellReuseIdentifier:paymentwayTableReuseId];
        [self.view addSubview:_paymentwayTable];
    }
    
    
    return _paymentwayTable;
}


- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"支付中心";
    [self configNewUI];
    [self layoutNewSubViews];
    [self getPayCenterPayInfo];
}


#pragma mark - 请求银联支付收款方式信息
- (void)getPayCenterPayInfo{
    
    [MyDataService requestNoRSAAFURL:KGET_DZERO_PAYMENT_PAY_INFO httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        
        SHLog(@"KGET_CAHS_PAY_INFO : %@",result);
        if ([result[@"success"] boolValue]) {
            
            self.paymentDataArray = [CSCustomPayWaysModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
            
        }
        
        /* 排序
        NSMutableArray *unionPayOtherArray = [NSMutableArray arrayWithCapacity:10];
        
        for (CSCustomPayWaysModel *model in unionPayArray) {
            if (model.status) {
                
                [self.paymentDataArray addObject:model];
                
            }else{
                
                [unionPayOtherArray addObject:model];
            }
            
            SHLog(@"%@",model);
        }
        for (CSCustomPayWaysModel *model in unionPayOtherArray) {
            
            [self.paymentDataArray addObject:model];
            
        }
        */
        [self initPaymentArray];
    } failure:^(id erro) {
        
    }];
    
    
}
#pragma mark - 设置支付方式
- (void)initPaymentArray{
    
    if ([self.using_type isEqualToNumber:@(1)]) {//D0充值+ 普通转账
        
        [MyDataService requestAFURL:@"/data/api/wholesale_Transfer_qualification" httpMethod:@"GET" params:nil data:nil complection:^(id result) {
            
            if ([result[@"success"] boolValue]) {
                
                if ([result[@"data"][@"status"] boolValue]) {
                    CSCustomPayWaysModel *unioPayModel = [[CSCustomPayWaysModel alloc]init];
                    unioPayModel.title = @"VIP转账";
                    unioPayModel.image = @"facepay";
                    unioPayModel.sub_title = @"大额转账,方便,快捷";
                    unioPayModel.selected = @(0);
                    unioPayModel.status = YES;
                    unioPayModel.red_point = NO;
                    unioPayModel.use_color = @"#f85154";
                    unioPayModel.use_title = @"确认支付";
                    unioPayModel.instructions = @"转账完成后,请一定要点击下方[支付成功?],否则金额不会到账;1小时内到账;如有疑问,请联系客服。";

                    unioPayModel.deposit_type = SHTRANSFERPAY;
                    [self.paymentDataArray addObject:unioPayModel];
                    
                    
                    [self.paymentwayTable reloadData];

                }
                
            }
        } failure:^(id erro) {
            
        }];
                
        
    }

    
//    customPaymentWay = SHUNIONQILINGPAY; //设置默认支付方式
    if ([self.using_type isEqual:@(20)] || [self.using_type isEqual:@(5)] ) {
        //云店购物 联盟商家 官方推广购物
        if ([self.using_type isEqual:@(5)]) {
            CSCustomPayWaysModel *balanceModel = [[CSCustomPayWaysModel alloc]init];
            balanceModel.image = @"balancepay";
            balanceModel.title = @"余额支付";
            balanceModel.sub_title = @"余额支付,安全，快捷";
            balanceModel.deposit_type = SHBALANCEPAY;
            balanceModel.status = YES;
            balanceModel.instructions = @"余额支付";
            balanceModel.use_color = @"#f85154";
            balanceModel.use_title = @"确认支付";
            [self.paymentDataArray addObject:balanceModel];
        }
       


    }
    [self.paymentwayTable reloadData];
    
}

#pragma mark - 返回
- (void)backAction:(UIButton *)btn{
   
//    for (int i = 0; i < self.rt_navigationController.rt_viewControllers.count; i ++) {
//        UIViewController *vc = self.rt_navigationController.rt_viewControllers[i];
//        if ([vc isKindOfClass:[CSOrderInfoViewController class]] || [vc isKindOfClass:[SettleViewController class]]) {
//            self.isSourceOrder = YES;
//            self.index = i;
//            break;
//        }
//    }
    
    if(self.isSourceOrder){
        
        [self.navigationController popToViewController:self.rt_navigationController.rt_viewControllers[self.index - 1] animated:YES];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
 
    }
    

    
}


- (void)configNewUI{
    
    self.view.backgroundColor = KBASEBGCOLOR;
    self.tabViewHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,__I6Height(102))];
    self.tabViewHeader.backgroundColor = KBASEBGCOLOR;
    self.paymentwayTable.tableHeaderView = self.tabViewHeader ;
    self.fistBgView = [[UIView alloc]init];
    self.fistBgView.backgroundColor = [UIColor whiteColor];
    [self.tabViewHeader addSubview:self.fistBgView];
    
    self.SecondBgView = [[UIView alloc]init];
    self.SecondBgView.backgroundColor = [UIColor whiteColor];
    [self.tabViewHeader addSubview:self.SecondBgView];
    
    self.paymentwayTable.tableHeaderView = self.tabViewHeader;
   
    
    self.paymoneyLabel = [[UILabel alloc]init];
    self.paymoneyLabel.text  = @"金额 :";
    self.paymoneyLabel.textColor = KCSTEXTCOLOR;
    self.paymoneyLabel.font = KSystemFont(10);
    [self.fistBgView addSubview:self.paymoneyLabel];
    
    self.orderidLabel = [[UILabel alloc]init];
    self.orderidLabel.text  = @"订单号 :";
    self.orderidLabel.textColor = KCSTEXTCOLOR;
    self.orderidLabel.font = KSystemFont(10);
    [self.SecondBgView addSubview:self.orderidLabel];
    
    self.paymoneyNumLabel = [[UILabel alloc]init];
    self.paymoneyNumLabel.text = [NSString stringWithFormat:@"¥ %@",self.total_money];
    self.paymoneyNumLabel.textColor = KCSREDTEXTCOLOR;
    self.paymoneyNumLabel.textAlignment = NSTextAlignmentRight;
    self.paymoneyNumLabel.font = KSystemFont(10);
    [self.fistBgView addSubview:self.paymoneyNumLabel];
    
    self.orderidNumLabel = [[UILabel alloc]init];
    if (!self.order_id) {
        
        self.orderidNumLabel.text = [NSString stringWithFormat:@"%@",self.orderPayId];

    }else{
        
        self.orderidNumLabel.text = [NSString stringWithFormat:@"%@",self.order_id];

    }
    
    self.orderidNumLabel.textAlignment = NSTextAlignmentRight;
    self.orderidNumLabel.textColor = KCSTEXTCOLOR;
    self.orderidNumLabel.font = KSystemFont(10);
    [self.SecondBgView addSubview:self.orderidNumLabel];
    
    UIView *topHeaderLine  = [[UIView alloc]initWithFrame:CGRectMake(0, __I6Height(45) - 0.5, KScreenWidth, 0.5)];
    topHeaderLine.backgroundColor = KLINECOLOR;
    [self.fistBgView addSubview:topHeaderLine];
    
    
    

}

- (void)layoutNewSubViews{
    
    [self.fistBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tabViewHeader.mas_top).with.offset(__I6Height(12));
        make.centerX.mas_equalTo(self.tabViewHeader.mas_centerX);
        make.width.mas_equalTo(KScreenWidth);
        make.height.mas_equalTo(__I6Height(45));
        
        
    }];
    [self.SecondBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fistBgView.mas_bottom).with.offset(0.5);
        make.centerX.mas_equalTo(self.tabViewHeader.mas_centerX);
        make.width.mas_equalTo(KScreenWidth);
        make.height.mas_equalTo(__I6Height(45));
        
        
    }];

    
    [self.paymoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.fistBgView.mas_left).with.offset(__I6Width(15));
        make.centerY.mas_equalTo(self.fistBgView.mas_centerY);
        make.width.mas_equalTo(__I6Width(80));
        make.height.mas_equalTo(__I6Width(20));
    }];
    
    [self.orderidLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.SecondBgView.mas_left).with.offset(__I6Width(15));
        make.centerY.mas_equalTo(self.SecondBgView.mas_centerY);
        make.width.mas_equalTo(__I6Width(80));
        make.height.mas_equalTo(__I6Width(20));
    }];
    
    [self.paymoneyNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.fistBgView.mas_right).with.offset(__I6Width(-15));
        make.centerY.mas_equalTo(self.fistBgView.mas_centerY);
        make.width.mas_equalTo(__I6Width(200));
        make.height.mas_equalTo(__I6Width(20));
    }];
    
    [self.orderidNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.SecondBgView.mas_left).with.offset(__I6Width(100));
        make.centerY.mas_equalTo(self.SecondBgView.mas_centerY);
        make.right.mas_equalTo(self.SecondBgView.mas_right).with.offset(__I6Width(-15));
            make.height.mas_equalTo(__I6Width(20));
    }];
    
    [self.paymentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.SecondBgView.mas_bottom).with.offset(__I6Width(20));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(KScreenWidth);
        make.height.mas_equalTo(__I6Height(40));
    }];
    
    
}

#pragma mark - 支付方式列表
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return  self.paymentDataArray.count;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   return __I6Height(55);
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CSPaymentWayCell  *paymentWayCell = [tableView dequeueReusableCellWithIdentifier:paymentwayTableReuseId];
    paymentWayCell.customPayWaysModel = self.paymentDataArray[indexPath.row];
    paymentWayCell.delegate = self;
    return paymentWayCell;
        
        /*
        UITableViewCell * payBtncell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        payBtncell.selectionStyle = UITableViewCellSelectionStyleNone;
        SHButton *payBtn = [SHButton buttonWithType:UIButtonTypeCustom title:@"确认支付" font:15 titleColor:[UIColor whiteColor] textAlignment:0 forState:UIControlStateNormal];
        payBtn.backgroundColor  = KREDCOLOR;
        payBtn.layer.cornerRadius = __I6Width(8);
        payBtn.layer.masksToBounds = YES;
        [payBtn addTarget:self action:@selector(payBtnClickerd:) forControlEvents:UIControlEventTouchUpInside];
        [payBtncell addSubview:payBtn];
        [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(__I6Height(42)));
            make.width.equalTo(@(__I6Width(326)));
            make.centerX.centerY.equalTo(payBtncell);
            make.top.mas_equalTo(payBtncell.mas_top).with.offset(__I6Height(40));
           }];
        return payBtncell;
    }
   */
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return __I6Height(57);
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    CSPaymentWayHeader *sectionHeader = [[CSPaymentWayHeader alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(57))];
    sectionHeader.backgroundColor = KBASEBGCOLOR;
       
    return sectionHeader;
    
   
}


#pragma mark - 选中支付方式
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    });
    
    CSCustomPayWaysModel * customPayWaysModel = self.paymentDataArray[indexPath.row];
    if (!customPayWaysModel.status) {
       
        [Tool alertWithMessage:customPayWaysModel.maintenance title:customPayWaysModel.use_title who:self];
       
        return;
    }
   
    customPaymentWay  = customPayWaysModel.deposit_type;
    if(customPaymentWay == 72){
        _qrcode_url = customPayWaysModel.qrcode_url;
    }
    self.webPayTitle = customPayWaysModel.title;
    
    [self beginToPay];
    
}

#pragma mark - 支付中心，发起支付
- (void)beginToPay{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.bezelView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [hud hideAnimated:YES afterDelay:1];
    
    if(customPaymentWay == SHBALANCEPAY){//余额支付
        
        [self requsteBalancePay];
        
    }else if(customPaymentWay == SHTRANSFERPAY){//普通转账
        
        CSTransferPayController *transferPayVc = [[CSTransferPayController alloc]init];
        transferPayVc.amount = self.total_money;
        [self.navigationController pushViewController:transferPayVc animated:YES];
        
    }else if(customPaymentWay == SHScanQrcodePay){
    
        
    }else if(customPaymentWay == SHALIPAY){
        
        [self userDoPay:@(customPaymentWay)];

        
    }else if(customPaymentWay == SHJDPAY){
        
          [self userDoPay:@(customPaymentWay)];

    }else{
        
        PayBankListView *payBankListView = [[PayBankListView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight) usingChoices:@(2) bankCardType:12  showAllBankCard:YES];
        payBankListView.delegate = self;
        
        [payBankListView showCustomView];
        
    }
    

}

/*
 SHD0 = 1,//充值到D0
 SHBALANCE = 2,//充值到余额
 SHGOSHOPPING = 5,//购物
 SHCASHER = 10,//取现
 SHDEPISIT = 15,//押金缴费
 SHSTORE = 20//云店购物
 
 
 */
#pragma mark -  获取用途
- (NSString *)getUsingType:(NSNumber *)usingType{
    
    switch ([usingType intValue]) {
        case 1:
            
            return @"D0充值";
        case 2:
            
            return @"余额充值";
            
        case 5:
            
            return @"商品购物";
            
        case 10:
            
            return @"提现";
        case 15:
            
            return @"押金";
            
        case 20:
            
            return @"云店购物";
            
            
            
        default:
            break;
    }
    return @"";
    
}



/*
 SHBALANCEPAY = 60, //余额支付
 SHUNIONPAY = 5,//银联支付
 SHWXPAY = 10,//微信扫码支付
 SHALIPAY = 20,//支付宝扫码支付
 SHJDPAY
 
 */

#pragma mark -- 用户统一支付
- (void)userDoPay:(NSNumber *)paymentType{
    
    if ([paymentType isEqual:@(0)]) {
        [self.view makeToast:@"请选择支付方式" duration:1.5 position:@"center"];
        
        return;
    }
    if ([self.using_type isEqual:@(0)]) {
        [self.view makeToast:@"用途为空" duration:1.5 position:@"center"];
        return;
        
    }
    if ([self.orderPayId isEqual:@(0)]) {
        [self.view makeToast:@"订单ID为空" duration:1.5 position:@"center"];
        return;
    }
    
   MBProgressHUD *hud =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hud hideAnimated:YES afterDelay:1.5];

    NSDictionary *params = @{@"payment_type":paymentType,@"using_type":self.using_type,@"order_id":self.orderPayId};
    SHLog(@"%@",params);
    [MyDataService requestAFURL:KCSUSER_DO_PAYMENT  httpMethod:@"POST" params:params data:nil complection:^(id result) {

        [MBProgressHUD hideHUDForView:self.view animated:YES];
        id data =   result[@"data"];
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            
            if ([paymentType isEqual: @(SHWXPAY)]) {//微信支付
                
                if ([data isKindOfClass:[NSString  class]]) {
                    
                    [self requsteWxScanQrcodePay:data];
                }
                
            }else if([paymentType isEqual: @(SHJDPAY)]){//京东支付
                if ([data isKindOfClass:[NSString  class]]) {

                    [self requsteJDPay:data];

                }
                
                
            }else if ([paymentType isEqual: @(SHALIPAY)]){//支付宝支付
                
                if ([data isKindOfClass:[NSString  class]]) {

                    [self requsteAlipayScanQrcodePay:data];
                    
                }

                
            }else if([paymentType isEqual: @(SHBALANCEPAY)]){// 余额支付

                [self.view makeToast:@"支付成功" duration:1.5 position:@"center"];
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延迟执行时间*/ * NSEC_PER_SEC));
                
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    [self backAction:nil];
                    //[self.navigationController popToRootViewControllerAnimated:YES];
                    
                });
                
                
            }

            
        }else{
            
            
            [self.view makeToast:result[@"data"] duration:2.0f position:@"center"];
        }
    } failure:^(id erro) {
        SHLog(@"%@",erro);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];

    
    
}


#pragma mark - 微信快捷支付
- (void)requsteWxQuickPay{
    
    if (!self.total_money) {
        SHLog(@"支付金额为空");
        return;
    }
    NSDictionary *params = @{@"amount":self.total_money};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:KYINGHSANG_WECHATPAY_RECHARGE  httpMethod:@"POST" params:params data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        SHLog(@"=====%@",result);

        if ([result[@"success"] boolValue]) {
            [self requsteWxScanQrcodePay:result[@"data"]];
//            CSQrcodeWebViewController *baseToolVc = [[CSQrcodeWebViewController alloc]init];
//            baseToolVc.urlString = result[@"data"];
//            baseToolVc.navigationItem.title = @"微信支付";
//            [self.navigationController pushViewController:baseToolVc animated:YES];
//
       
        }else{
            [self.view makeToast:result[@"data"] duration:2 position:@"center"];
        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
    
    
}

#pragma mark - 支付宝快捷支付
- (void)requsteAlipayQuickPay{
    
    if (!self.total_money) {
        SHLog(@"支付金额为空");
        return;
    }
    NSDictionary *params = @{@"amount":self.total_money};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:KYINGHSANG_ALIPAY_RECHARGE httpMethod:@"POST" params:params data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        SHLog(@"=====%@",result);
        
        if ([result[@"success"] boolValue]) {
            CSQrcodeWebViewController *baseToolVc = [[CSQrcodeWebViewController alloc]init];
            baseToolVc.urlString = result[@"data"];
            baseToolVc.navigationItem.title = @"支付宝支付";
            [self.navigationController pushViewController:baseToolVc animated:YES];
            
            
        }else{
            
            [self.view makeToast:result[@"data"] duration:2 position:@"center"];

        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
    
    
    
}

#pragma mark -  微信扫码维码支付
- (void)requsteWxScanQrcodePay:(NSString *)qrcodeUrlStr{

   [self.rt_navigationController removeViewController:self];

}

#pragma mark -  支付宝扫码维码支付
- (void)requsteAlipayScanQrcodePay:(NSString *)qrcodeUrlStr{

    [self.rt_navigationController  removeViewController:self];

}


#pragma mark -  京东快捷支付
-(void)requsteJDPay:(NSString *)jdhtml{
    
    
    CSJDPayViewController *jdPay = [[CSJDPayViewController alloc]init];
    jdPay.jdhtml = jdhtml;
    [self.navigationController pushViewController:jdPay animated:YES];
    [self.rt_navigationController  removeViewController:self];

}

#pragma mark -  余额支付 检查是否设置支付密码
- (void)requsteBalancePay{
    
    [MyDataService requestAFURL:Klogin  httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        if ([result[@"success"]isEqualToNumber:@(1)] && [result[@"data"] count] != 0 ) {
            UserModel *userModel = [UserModel shareManager];
            userModel =  [UserModel mj_objectWithKeyValues:result[@"data"]];
            if (!userModel.has_pay_password) {
                SHUserPayPasswordController *setPsVc = [[SHUserPayPasswordController alloc]init];
                [self.navigationController pushViewController:setPsVc animated:YES];
            }else{
                [self checkBalance];
            }
            
        }
    } failure:^(id erro) {
        [self.view makeToast:@"拼命链接中，稍等哈" duration:1.5 position:@"center"];

    }];


}

#pragma mark - 检查余额
- (void)checkBalance{
    
    [MyDataService requestAFURL:KUSER_GET_CURRENCY httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            //联商红包
            NSNumber *pay_balance = result[@"data"][@"pay_balance"];
            self.balance = [NSString stringWithFormat:@"%.2f",[pay_balance floatValue]];
            
            if ([self.total_money floatValue] > [self.balance floatValue]) {
                [self.view makeToast:@"消费余额不足" duration:2 position:@"center"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController pushViewController:[[ToppedCentreViewController alloc]init] animated:YES];
                });
                
                return;
            }
            self.passwordView = [[PassWordView  alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
            self.passwordView.delegate = self;
            [[UIApplication sharedApplication].keyWindow addSubview:self.passwordView];
            
            
        }
    } failure:^(id erro) {
        [self.view makeToast:@"拼命链接中，稍等哈" duration:1.5 position:@"center"];

    }];
    

}
#pragma mark -- 密码框代理
- (void)useStoreCode:(NSString *)code
{
    [UIView animateWithDuration:0.5 animations:^{
        self.passwordView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.passwordView removeFromSuperview];
    }];
    //验证码密码
    NSLog(@"支付密码---%@：",code);
    NSDictionary *params = @{@"pay_password": code};
    [MyDataService requestAFURL:KUSERCheck_Pay_PassWord httpMethod:@"POST" params:params data:nil complection:^(id result) {
        if ([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] isEqualToString:@"ok"]) {
            
        
            [self userDoPay:@(SHBALANCEPAY)];
            
            
        }else{

            [self.view makeToast:result[@"data"] duration:2 position:@"center"];
        
        }
    } failure:^(id erro) {
        
        [self.view makeToast:@"拼命链接中，稍等哈" duration:1.5 position:@"center"];

    }];
    
    
    
    
    
}
- (void)disAction
{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.1 animations:^{
        self.passwordView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.passwordView removeFromSuperview];
    }];
    
}



#pragma mark -  银联支付弹出银行卡的代理方法

- (void)payBankListView:(PayBankListView *)view bankCardCode:(NSString *)bankCardCode bankCardId:(NSString *)bankCardId usingChoices:(NSNumber *)usingChoices{
    
    if (![bankCardCode isEqualToString:@"0"]) {
        
        
        
        if ([self.using_type isEqual:@(0)]) {
            [self.view makeToast:@"用途为空" duration:1.5 position:@"center"];
            return;
            
        }
        if ([self.orderPayId isEqual:@(0)]) {
            [self.view makeToast:@"订单ID为空" duration:1.5 position:@"center"];
            return;
        }

        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary *params = @{@"payment_type":@(customPaymentWay),@"using_type":self.using_type,@"order_id":self.orderPayId,@"bank_card_code":bankCardCode};
        [MyDataService requestAFURL:KUSER_DO_PAYMENT  httpMethod:@"POST" params:params data:nil complection:^(id result) {
            SHLog(@"KUSER_DO_PAYMENT params:%@  %@",params,result);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if ([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] isKindOfClass:[NSDictionary class]]) {
                
                JDPayViewController *jdPay = [[JDPayViewController alloc]init];
                jdPay.qiuckPayDict = result[@"data"];
                jdPay.type = 2;
                jdPay.navigationItem.title = self.webPayTitle;

                /*
                if(customPaymentWay == SHUNIONQILINGPAY){
                    
                    [MobClick event:@"pay_center_union"];

                }else{
                    
                    jdPay.navigationItem.title =                     jdPay.navigationItem.title = @"银联支付";
                    ;
                    [MobClick event:@"pay_center_union_fht"];


                    
                }
                 */
                [self.navigationController pushViewController:jdPay animated:YES];
                [self.rt_navigationController  removeViewController:self];

                
            }else{
                if ([result[@"data"] isKindOfClass:[NSString class]]) {
                    [self.view makeToast:result[@"data"] duration:1.5 position:@"center"];
                }
                
            }
        } failure:^(id erro) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.view makeToast:@"拼命链接中，稍等哈" duration:1.5 position:@"center"];

            
        }];
        
        
        
    }else{
        
        SHAddPayBankCardController *add = [[SHAddPayBankCardController alloc]init];
        [self.navigationController pushViewController:add animated:YES];
        
    }
  
    
}


#pragma mark - 点击右侧
- (void)paymentWayCell:(CSPaymentWayCell *)cell showRateMsg:(NSString *)rateMsg{
    
    [Tool alertWithMessage:rateMsg title:@"使用说明" who:self];
   
}

@end
